﻿using System.Collections.Generic;
using CommonFoundation.DataModel;
using System.IO;
using CommonFoundation.DataSource;
using System;
using System.Runtime.Serialization.Formatters.Binary;

namespace DataCreator
{
    public class DataCreator
    {
        static void Main(string[] args)
        {
            DataCreator dataCreator = new DataCreator();

            dataCreator.ReadInData();

            dataCreator.Save();
        }

        public void ReadInData()
        {
            using (StreamReader streamReader = new StreamReader(FilePath.GetInstance().AllLessonsFilePathForDataCreator))
            {
                while (true)
                {
                    string content = streamReader.ReadLine();

                    if (String.IsNullOrEmpty(content))
                    {
                        break;
                    }

                    string[] infos = content.Split('|');

                    int startIndex = -1;


                    foreach (string weekDay in Consts.WeekDayNames)
                    {
                        for (int i = 0; i<infos.Length; i++)
                        {
                            if (infos[i].Equals(weekDay))
                            {
                                startIndex = i;
                                break;
                            }
                        }

                        if (startIndex != -1)
                        {
                            break;
                        }
                    }

                    if (startIndex == -1)
                    {
                        continue;
                    }


                    Lesson lesson = new Lesson();

                    //现代数学专题选讲（3）|许全华|教授|数学与统计学院| |专业选修|2.0|数学基地班|2008|数学与统计学院|40|Mon|1-12周|Mon|1区|数-113|7-9节|
                    //太极与养生|蔺志华|副教授|体育部| |公共选修|1.0| |100|Tue|1-7周|Tue|1区|5-506|12-14节|
                    lesson.LessonName = infos[0];
                    lesson.TeacherName = infos[1];

                    //如果是非公选
                    if (startIndex == 11)
                    {
                        lesson.School = infos[9];
                    }
                   

                    while (startIndex + this.lengthForSchedule - 1< infos.Length)
                    {
                        Schedule schedule = new Schedule();

                        schedule.WeekDay = this.GetIndexForWeekDay(infos[startIndex]);

                        schedule.Weeks = infos[startIndex + 1];

                        schedule.Location = infos[startIndex + 3] + infos[startIndex + 4];

                        schedule.Time = infos[startIndex + 5];

                        lesson.Schedules.Add(schedule);

                        startIndex += this.lengthForSchedule;
                    }

                    this.lessons.Add(lesson);
                }
            }

        }

        private void Save()
        {

            using (Stream output = File.Create("Lesson.dat"))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();

                binaryFormatter.Serialize(output, this.lessons);
            }
        }

        private int lengthForSchedule = 6;
        private List<Lesson> lessons = new List<Lesson>();

        private int GetIndexForWeekDay(string value)
        {
            for (int i = 0; i < 7; i++)
            {
                if (Consts.WeekDayNames[i].Equals(value))
                {
                    return i;
                }
            }

            return -1;
        }
    }
}
